package com.iflytek.uc.dao.impl;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.iflytek.uc.dao.AccountDao;
import com.iflytek.uc.dao.EntityTypeDao;
import com.iflytek.uc.domain.Account;
import com.iflytek.uc.po.PoParty;

public class AccountDaoImpl extends HibernateDaoSupport implements AccountDao {
	
	//private static final Logger LOG = Logger.getLogger(AccountDaoImpl.class);

	private EntityTypeDao entityTypeDao;

	public void setEntityTypeDao(EntityTypeDao entityTypeDao) {
		this.entityTypeDao = entityTypeDao;
	}

	public Account getAccountByName(String name) {
		
		String hql = "from PoParty where name=:name";
		Query query = getSession().createQuery(hql);
		query.setString("name", name);
		
		PoParty party = (PoParty)query.uniqueResult();
		
		return convert(party);
	}

	private Account convert(PoParty party) {
		Account account = new Account();
		account.setChildCount(party.getChildCount());
		account.setDisabled(party.isDisabled());
		account.setDisplayName(party.getDisplayName());
		account.setDisplayOrder(party.getDisplayOrder());
		account.setEntityType(entityTypeDao.getEntityTypeByName(party.getEntityTypeName()));
		account.setGuid(party.getGuid());
		account.setName(party.getName());
		account.setParentCount(party.getParentCount());
		account.setPassword(party.getPassword());
		account.setPinyin(party.getPinyin());
		account.setShortPinyin(party.getShortPinyin());
		
		return account;
	}
}
